<?php

include __DIR__ . "/../include.php";

// 小程序配置
$config = [
    'appid'     => 'wx6bb7b70258da09c6',
    'appsecret' => '78b7b8d65bd67b078babf951d4342b42',
];

// $mini = \Weapp\Shop\Common::instance($config);
// print_r($mini->cat());
// die();

$mini = \Weapp\Shop\Spu::instance($config);
echo '<pre>';
/*
$data = [
    'out_product_id' => 100,
    'title' => '牙膏',
    'path' => 'pages/productDetail/productDetail?productId=2176180',
    'head_img' => ["http://img10.360buyimg.com/n1/s450x450_jfs/t1/85865/39/13611/488083/5e590a40E4bdf69c0/55c9bf645ea2b727.jpg"],
    'desc_info' => ['desc' => 'xxxxxx', 'imgs' => ["http://img10.360buyimg.com/n1/s450x450_jfs/t1/85865/39/13611/488083/5e590a40E4bdf69c0/55c9bf645ea2b727.jpg"]],
    'third_cat_id' => 1012,
    'brand_id' => 2100000000,
    'skus' => [
        [
            'out_product_id' => 100,
            'out_sku_id' => 101,
            'thumb_img' => 'http://img10.360buyimg.com/n1/s450x450_jfs/t1/100778/17/13648/424215/5e590a40E2d68e774/e171d222a0c9b763.jpg',
            'sale_price' => 10000,
            'market_price' => 20000,
            'stock_num' => 1000,
            'sku_attrs' => [
                ["attr_key" => "选择颜色", "attr_value" => "红蓝主机"],
                ["attr_key" => "选择套装", "attr_value" => "主机+保护套"]
            ]
        ]
    ]
];
$res = $mini->add($data);
*/
// $data = ['product_id' => 415513043, 'out_product_id' => 100, 'need_edit_spu' => 1];
// $res = $mini->get($data);
// print_r($res);

// $res = $mini->get_list(['page'=>1, 'need_edit_spu' => 1, 'page_size' => 10]);
// print_r($res);

$orderData = [
    "create_time" => date('Y-m-d H:i:s'),
    "out_order_id" => "1001",// 必填，普通场景下的外部订单ID；合单支付（多订单合并支付一次）场景下是主外部订单ID
    "openid" => "o2qzQ4lzmsin7m53g6Kgj28k-bUI",
    "path" => "/pages/order.html?out_order_id=1001",// 这里的path中的最好有一个参数的值能和out_order_id的值匹配上
    "scene" => 1177,// 下单时小程序的场景值，可通过[getLaunchOptionsSync](https://developers.weixin.qq.com/miniprogram/dev/api/base/app/life-cycle/wx.getLaunchOptionsSync.html)或[onLaunch/onShow](https://developers.weixin.qq.com/miniprogram/dev/reference/api/App.html#onLaunch-Object-object)拿到
    "order_detail" => [
        'product_infos' => [
            [
                "out_product_id" => "100",
                "out_sku_id" => "101",
                "product_cnt" => 1,
                "sale_price" => 100,   //生成这次订单时商品的售卖价，可以跟上传商品接口的价格不一致
                "real_price" => 100,  // 扣除优惠后单件sku的分摊价格（单位：分），如果没优惠则与sale_price一致
                "path" => "pages/productDetail/productDetail?productId=2176180",
                "title" => "牙膏",
                "head_img" => "http://img10.360buyimg.com/n1/s450x450_jfs/t1/85865/39/13611/488083/5e590a40E4bdf69c0/55c9bf645ea2b727.jpg",
            ]
        ],
        'pay_info' => [
            "pay_method_type" => 2,// 0: 微信支付, 1: 货到付款, 2: 商家会员储蓄卡（默认0）
        ],
        "price_info" => [// 注意价格字段的单价是分，不是元
            "order_price" => 1600,
            "freight" => 500,
        ],
        "delivery_detail" => [
            "delivery_type" => 1,// 1: 正常快递, 2: 无需快递, 3: 线下配送, 4: 用户自提
        ],
    ],
    "address_info" => [
        "receiver_name" => "王伟",
        "detailed_address" => "江陵路323号",
        "tel_number" => "18777656556",
        "country" => "中国",
        "province" => "浙江省",
        "city" => "杭州市",
        "town" => "滨江区"
    ]
];
/*
$mini = \Weapp\Shop\Order::instance($config);
// print_r($mini->add($orderData));

$data = [
    "order_id" => 2131425237861711880,
    "openid" => "o2qzQ4lzmsin7m53g6Kgj28k-bUI",
    "action_type" => 1, 
];
print_r($mini->pay($data));


$data = [
    "order_id" => 2131425237861711880,
    "openid" => "o2qzQ4lzmsin7m53g6Kgj28k-bUI",
    "out_order_id" => '1001'
];
print_r($mini->get($data));

$mini = \Weapp\Shop\Delivery::instance($config);
// print_r($mini->get_company_list());
$data = [
    "order_id" => 2131425237861711880,
    "openid" => "o2qzQ4lzmsin7m53g6Kgj28k-bUI",
    "finish_all_delivery" => 1,// 0: 未发完, 1:已发完
    "delivery_list" => [
        [
            "delivery_id" => "SF",
            "waybill_id" => "23424324253"
        ]
    ]
];
print_r($mini->send($data));
*/

$return = [
    "out_order_id" => "1001",
    "out_aftersale_id" => "1001",   // 商家售后ID
    "openid" => "o2qzQ4lzmsin7m53g6Kgj28k-bUI",
    "type" => 1,          // 1:退款,2:退款退货,3:换货
    "create_time" => date('Y-m-d H:i:s'),
    "status" => 13, 
    "finish_all_aftersale" => 1, 
    "path" => "/pages/aftersale.html?out_aftersale_id=xxxxx",
    "refund" => 100,
    "product_infos" => [
        [
            "out_product_id" => "100",
            "out_sku_id" => "101",
            "product_cnt" => 1
        ]
    ]
];

$mini = \Weapp\Shop\AfterSale::instance($config);
print_r($mini->add($return));